capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local crime "";
local output "";
local zip "" ;

/* 
RD estimates by Medicaid and FQHC availability 
*/


local exp "prworavary";

local controls_all "n_male i.n_firstage i.n_diag1cat n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_fam_singmom n_fam_nopar i.n_earnhhbins" ; /* i.state */
local controls_no  "" ; 

local bwidth_list "264" ;
local cov_list   "all" ; 

local poly_list "1(1)1" ;


/* ********************************* */
/* **** PREP MEDICAID/FQHC DATA **** */
/* ********************************* */

use "`zip'\pdscc_fips.dta", clear ;
drop if pdscc_num == "" ;
destring pdscc_num, gen(temp) ;
gen str5 pdscc = string(temp,"%05.0f") ;
tempfile pdscc ;
save `pdscc' ;

use "`crime'\medicaid_states.dta", clear ; /* We prepared the Medicaid data by states using the information from Rudowitz et al. (2013) who collects data from states that offer Medicaid to young adults pre-ACA (through Section 1115 waivers) and post-ACA, for the period 1994-2021 */
destring FIPS, gen(fips_state) ;
rename Medicaid medicaid ;
tempfile medicaid ;
save `medicaid' ;

use "`crime'\fqhc_counties.dta", clear ; /* We prepared this data combining information from two distinct sources: Uniform Data Set (UDS) and Provider of Service (POS) files from 1996-2018. Colin Gray shared the UDS data with us, and the POS files are publicly available online: https://www.nber.org/data/provider-of-services.html */
gen fips_cnty = substr(county,3,3) ;
destring fips_cnty, replace ;
rename fips_state_cd fips_state ;
rename FQHC fqhc ;
tempfile fqhc ;
save `fqhc' ;


/* ************************************** */
/* **** RUN YEAR-BY-YEAR REGRESSIONS **** */
/* ************************************** */

use "`crime'\prep2\crime_prwora_prepforreg.dta", clear ;

keep if abs(dobdd_run) <= `bwidth_list' ;

keep if keep_1997_2017_6cg == 1 ;

keep hun *yr*genr* *yr*ahinc*
	dobdd_run n_post n_age18unfav
	n_male n_firstage n_diag1cat n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_fam_singmom n_fam_nopar n_earnhhbins state 
	pdscc* ;

reshape long yr_crime2genr_ n_yrcrime2ahinc_, i(hun) j(year) ;
tab year, m ;
rename yr_crime2genr_ crime2genr ;
rename n_yrcrime2ahinc_ crime2ahinc ;
sort hun year ;
foreach stub in genr ahinc { ;
	by hun: gen crime2`stub'_pre1  = crime2`stub'[_n-1] ;
	by hun: gen crime2`stub'_post1 = crime2`stub'[_n+1] ;
} ;

merge m:1 pdscc using `pdscc', keepusing(fips_state fips_cnty) ;
drop if _merge == 2 ;
drop _merge ;

/* Merge in Medicaid */
merge m:1 fips_state year using `medicaid', keepusing(medicaid) ;
drop if _merge == 2 ;
drop _merge ;

merge m:1 fips_state fips_cnty year using `fqhc', keepusing(fqhc) ;
drop if _merge == 2 ;
replace fqhc = 0 if _merge == 1 & fqhc == . ;
drop _merge ;

local abbr_medicaid "med" ;
local abbr_fqhc "fqhc" ;

gen postXmedicaid = n_post * medicaid ;
gen postXfqhc = n_post * fqhc ;

local medicaid_last = 2019 ;
local fqhc_last = 2018 ;

local cond_gr	"& year >= 2009 & year <=2012" ;
local cond_allyr	"" ;
	
/* RD variables */
gen dobdd_run2=dobdd_run*dobdd_run;
gen dobdd_run3=dobdd_run2*dobdd_run;
gen dobdd_run4=dobdd_run3*dobdd_run;

gen postXdobdd=n_post*dobdd_run;
gen postXdobdd2=n_post*dobdd_run2;
gen postXdobdd3=n_post*dobdd_run3;
gen postXdobdd4=n_post*dobdd_run4;

local poly_1 "dobdd_run postXdobdd";
local poly_2 "dobdd_run dobdd_run2 postXdobdd postXdobdd2";
local poly_3 "dobdd_run dobdd_run2 dobdd_run3 postXdobdd postXdobdd2 postXdobdd3";
local poly_4 "dobdd_run dobdd_run2 dobdd_run3 dobdd_run4 postXdobdd postXdobdd2 postXdobdd3 postXdobdd4";

foreach demo in all { ;

foreach bwidth in `bwidth_list' {;

	forval poly = `poly_list' {;
		
		foreach cov in `cov_list' { ;
				
					foreach rate in fqhc medicaid { ;
					
							foreach time in /*gr*/ allyr { ;
							
								xi: reg n_post 
									postX`rate' n_post  dobdd_run postXdobdd 
									if abs(dobdd_run)<=`bwidth' & year <= ``rate'_last'
									, cluster(hun) robust; 
								outreg2 using "`output'\\`exp'_`rate'_`time'_`bwidth'`demo'`poly'`cov'.xls", e(N r2 rmse) replace ;
								
							
								foreach out in crime2genr { ;
								
									xi: reg `out' 
										n_post dobdd_run postXdobdd 
										`controls_`cov''
										if abs(dobdd_run)<=`bwidth' & year <= ``rate'_last' `cond_`time''
										, cluster(hun) robust; 
									outreg2 using "`output'\\`exp'_`rate'_`time'_`bwidth'`demo'`poly'`cov'.xls", e(N r2 rmse) append ;
									
									xi: reg `out' 
										postX`rate' n_post  dobdd_run postXdobdd 
										`rate' 
										`controls_`cov''
										if abs(dobdd_run)<=`bwidth' & year <= ``rate'_last' `cond_`time''
										, cluster(hun) robust; 
									outreg2 using "`output'\\`exp'_`rate'_`time'_`bwidth'`demo'`poly'`cov'.xls", e(N r2 rmse) append ;
									
									xi: reg `out' 
										postX`rate' n_post dobdd_run postXdobdd 
										`rate' i.year
										`controls_`cov''
										if abs(dobdd_run)<=`bwidth' & year <= ``rate'_last' `cond_`time''
										, cluster(hun) robust; 
									outreg2 using "`output'\\`exp'_`rate'_`time'_`bwidth'`demo'`poly'`cov'.xls", e(N r2 rmse) append ;
										
									xi: reg `out' 
										postX`rate' n_post dobdd_run postXdobdd 
										`rate' i.year i.state 
										`controls_`cov''
										if abs(dobdd_run)<=`bwidth' & year <= ``rate'_last' `cond_`time''
										, cluster(hun) robust; 
									outreg2 using "`output'\\`exp'_`rate'_`time'_`bwidth'`demo'`poly'`cov'.xls", e(N r2 rmse) append ;
									
									xi: reg `out' 
										postX`rate' n_post dobdd_run postXdobdd 
										`rate' i.year i.pdscc 
										`controls_`cov''
										if abs(dobdd_run)<=`bwidth' & year <= ``rate'_last' `cond_`time''
										, cluster(hun) robust; 
									outreg2 using "`output'\\`exp'_`rate'_`time'_`bwidth'`demo'`poly'`cov'.xls", e(N r2 rmse) append ;
									
									
							} ; /* end out */ 
						} ; /* end time */
					} ; /* end rate */

											
			} ; /* end cov */
				
		} ; /* end poly */
	} ; /* end bwidth*/
} ;	/* end demo */

capture log close;
